package com.ruoyi.web.controller.system;

import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.SysTokenRequest;
import com.ruoyi.system.service.ISysTokenService;

/**
 * Token管理Controller
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/system/token")
public class SysTokenController extends BaseController
{
    @Autowired
    private ISysTokenService sysTokenService;

    /**
     * 获取Token
     */
    @PostMapping("/getToken")
    public AjaxResult getToken(@RequestBody SysTokenRequest request)
    {
        try
        {
            Map<String, Object> tokenInfo = sysTokenService.getTokenWithCache(
                request.getUsername(), 
                request.getPassword(), 
                request.getApiUrl()
            );
            return AjaxResult.success(tokenInfo);
        }
        catch (Exception e)
        {
            logger.error("获取Token失败", e);
            return AjaxResult.error("获取Token失败: " + e.getMessage());
        }
    }
    
    /**
     * 清空所有Token缓存
     */
    @PostMapping("/clearAllCache")
    public AjaxResult clearAllCache()
    {
        try
        {
            boolean result = sysTokenService.clearAllTokenCache();
            if (result)
            {
                return AjaxResult.success("所有Token缓存已清空");
            }
            else
            {
                return AjaxResult.error("清空Token缓存失败");
            }
        }
        catch (Exception e)
        {
            logger.error("清空Token缓存失败", e);
            return AjaxResult.error("清空Token缓存失败: " + e.getMessage());
        }
    }
    
    /**
     * 清空指定Token缓存
     */
    @PostMapping("/clearCache")
    public AjaxResult clearCache(@RequestBody SysTokenRequest request)
    {
        try
        {
            boolean result = sysTokenService.clearTokenCache(
                request.getUsername(), 
                request.getApiUrl()
            );
            if (result)
            {
                return AjaxResult.success("Token缓存已清空");
            }
            else
            {
                return AjaxResult.error("清空Token缓存失败");
            }
        }
        catch (Exception e)
        {
            logger.error("清空Token缓存失败", e);
            return AjaxResult.error("清空Token缓存失败: " + e.getMessage());
        }
    }
    
    /**
     * 重刷Token缓存
     */
    @PostMapping("/refreshCache")
    public AjaxResult refreshCache(@RequestBody SysTokenRequest request)
    {
        try
        {
            Map<String, Object> tokenInfo = sysTokenService.refreshTokenCache(
                request.getUsername(), 
                request.getPassword(), 
                request.getApiUrl()
            );
            return AjaxResult.success("Token缓存已刷新", tokenInfo);
        }
        catch (Exception e)
        {
            logger.error("刷新Token缓存失败", e);
            return AjaxResult.error("刷新Token缓存失败: " + e.getMessage());
        }
    }
}